package com.booter.common;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.management.MBeanServer;
import javax.management.MBeanServerFactory;
import javax.management.ObjectName;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Set;

public final class NetworkUtils {
    public static String ipv4 = "(?=(\\b|\\D))(((\\d{1,2})|(1\\d{1,2})|(2[0-4]\\d)|(25[0-5]))\\.){3}((\\d{1,2})|(1\\d{1,2})|(2[0-4]\\d)|(25[0-5]))(?=(\\b|\\D))";
    public static String ipv6 = "^([\\\\da-fA-F]{1,4}:){7}([\\\\da-fA-F]{1,4})$";
    private static Logger logger = LoggerFactory.getLogger(NetworkUtils.class);

    private NetworkUtils() {
    }

    public static String getLocalIP() {
        String sIP = "";
        InetAddress element = null;
        try {
            if (NetworkUtils.isWindowsOS()) {
                element = InetAddress.getLocalHost();
            } else {
                boolean bFindIP = false;
                Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
                while (netInterfaces.hasMoreElements()) {
                    if (bFindIP) {
                        break;
                    }
                    NetworkInterface ni = netInterfaces.nextElement();
                    if (ni.getDisplayName().toLowerCase()
                            .contains("Bluetooth".toLowerCase())
                            || ni.getName().toLowerCase().contains("docker")
                            || ni.getDisplayName().contains("VMware")
                            || ni.getDisplayName().contains("Virtual")) {
                        continue;
                    }
                    Enumeration<InetAddress> address = ni.getInetAddresses();
                    while (address.hasMoreElements()) {
                        element = address.nextElement();
                        if (element instanceof Inet4Address) {
                            bFindIP = true;
                            break;
                        }
                    }
                }
            }
        } catch (Exception e) {
            logger.error("NetworkUtils getLocalIP error:", e);
        }
        if (null != element) {
            sIP = element.getHostAddress();
        }
        return sIP;
    }

    public static boolean isWindowsOS() {
        boolean isWindowsOS = false;
        String osName = System.getProperty("os.name");
        if (osName.toLowerCase().indexOf("windows") > -1) {
            isWindowsOS = true;
        }
        return isWindowsOS;
    }

    public static String getHttpPort() {
        MBeanServer mBeanServer = null;
        if (MBeanServerFactory.findMBeanServer(null).size() > 0) {
            mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0);
        }
        Set<ObjectName> names = null;
        try {
            if (mBeanServer != null) {
                names = mBeanServer.queryNames(
                        new ObjectName("Catalina:type=Connector,*"), null);
            }
            if (names != null && mBeanServer != null) {
                Iterator<ObjectName> it = names.iterator();
                ObjectName oname = null;
                String port = "";
                while (it.hasNext()) {
                    oname = it.next();
                    String pvalue = (String) mBeanServer.getAttribute(oname,
                            "protocol");
                    String svalue = (String) mBeanServer.getAttribute(oname,
                            "scheme");
                    logger.info(
                            "==============Catalina:type[{}][{}][{}][{}]",
                            oname.toString(), pvalue, svalue,
                            mBeanServer.getAttribute(oname, "port"));
                    if ("http".equals(svalue)) {
                        if (pvalue.contains("http11")) {
                            return ((Integer) mBeanServer.getAttribute(oname,
                                    "port")).toString();
                        } else if (pvalue.startsWith("HTTP")) {
                            port = ((Integer) mBeanServer.getAttribute(oname,
                                    "port")).toString();
                        }
                    }
                }
                return port;
            }
        } catch (Exception e) {
            logger.error("NetworkUtils getHttpPort error:", e);
            return "";
        }
        return "";
    }
}

